package agh.io.api {
	import com.google.maps.LatLngBounds;	
	
	import flash.display.LoaderInfo;	
	import flash.events.MouseEvent;
	import agh.io.base.ResultMarker;
	import agh.io.js.JsController;
	import agh.io.model.ResultEvent;
	import agh.io.xmlCommunicator.Communicator;
	import agh.io.xmlCommunicator.XmlParser;

	import com.google.maps.InfoWindowOptions;
	import com.google.maps.LatLng;
	import com.google.maps.Map;
	import com.google.maps.MapEvent;
	import com.google.maps.MapMouseEvent;
	import com.google.maps.MapType;
	import com.google.maps.controls.MapTypeControl;
	import com.google.maps.controls.PositionControl;
	import com.google.maps.controls.ZoomControl;
	import com.google.maps.overlays.Marker;

	import flash.display.Sprite;
	import flash.events.Event;
	import flash.external.ExternalInterface;
	import flash.geom.Point;
	import flash.text.TextField;

	/**
	 * @author endq
	 */
	public class MapAdapter extends Sprite implements IMapAdapter {
		
		protected var map : Map;
		protected var mapContainer : Sprite;
		
		protected var jsController : JsController;
		
		protected var communicator : Communicator;
		
		public var markerList : Array = new Array();
		
		public var resultList : Array;
		
		public var locationString:String;
		
		public static var status : TextField = new TextField();
		
		public function MapAdapter() {
			
			mapContainer =  new Sprite();
			this.addChild(mapContainer);	
			
			map = new Map();
			map.key = "ABQIAAAAeLe7ijfCeAq1sUhc-8T9hRQeUIooRUBo-V-P2q6Xdfyz8KzZxhShnr0hKRxkr8Fkb56VhGrWDQL2pQ";
			map.sensor = "false";
			map.setSize(new Point(550, 450));
			map.addEventListener(MapEvent.MAP_READY, onMapReady);
			mapContainer.addChild(map);		
			
			status.textColor = 0xFF0000;
			status.width = 100;
			status.x = 400;
			status.height = 20;
			status.border = false;
			status.text = "";
			this.addChild(status);
			//status.addEventListener(MouseEvent.CLICK, onc);
		}
		
		//protected function onc(event : MouseEvent) : void {
		//	showEventLocatation("3");
		//}
		
		protected function onMapReady(event : Event) : void {
			var obj:Object = LoaderInfo(this.root.loaderInfo).parameters;
			Communicator.SERVER_INTERFACE_URL = String(obj.wsurl);
			locationString = String(obj.location);
			
			if(locationString=="k"){
				map.setCenter(new LatLng(50.065404096015, 19.94233092907713), 10, MapType.NORMAL_MAP_TYPE);
			}else{
				map.setCenter(new LatLng(52.00331185482757, 18.952424141205015), 6, MapType.NORMAL_MAP_TYPE);
			}
			
			map.addControl(new ZoomControl());
			map.addControl(new PositionControl());
			map.addControl(new MapTypeControl());	
			
			//status.text = Communicator.SERVER_INTERFACE_URL;
			communicator = new Communicator(this);
			communicator.SetupWebService();
			
			jsController = new JsController(this, communicator);					
		}		
		
		public function edit() : void {
			map.addEventListener(MapMouseEvent.CLICK, onMapClick);  
			
			communicator.Search("", "", "");
		}
		
		public function cancelEdit() : void {
			map.removeEventListener(MapMouseEvent.CLICK, onMapClick);  
			clear();
		}
		
		public var tmpMarker:Marker;
		
		protected function onMapClick(event : MapMouseEvent) : void {
			if(tmpMarker!=null){
				map.removeOverlay(tmpMarker);
			}
			
	 		tmpMarker = new Marker(event.latLng);
	 		map.addOverlay(tmpMarker);
	 		
	 		ExternalInterface.call("callbackInsertPoint", XmlParser.GeneratePointXml(event.latLng.lat(), event.latLng.lng()));
		}
		
		public function displaySearch(resultList: Array) : void {
			
			clear();
			
			if(resultList.length==0){
				return;
			}
			
			this.resultList = resultList;
			
			buildMarkers(this.resultList);
		}		
		
		public function buildMarkers(rl:Array) : void {
			var llArray:Array = new Array();
			var i:int =0;
			for each(var re:ResultEvent in rl) {
				llArray[i] = new LatLng(re.x, re.y);
				var m : Marker = new Marker(llArray[i]);
	 			map.addOverlay(m);
	 			var marker:ResultMarker = new ResultMarker(m, re);
	 			markerList.push(marker);
	 			i++;
			}
			
			for each(var rm:ResultMarker in markerList) {
				rm.marker.addEventListener(MapMouseEvent.CLICK, clickMarker); 
			}
			
			var llb:LatLngBounds = new LatLngBounds(); 
			for (i = 0; i < llArray.length; i++ )
			{
			  llb.extend(llArray[i]);
			}
			
			map.setCenter(llb.getCenter(), map.getBoundsZoomLevel(llb));
			
			//map.setCenter(markerList[0].marker.getLatLng(), 10, MapType.NORMAL_MAP_TYPE);
			//Marker(markerList[0].marker).openInfoWindow(new InfoWindowOptions({title: markerList[0].resultEvent.locationName, content: markerList[0].resultEvent.locationAdress}));
		}
		
		public function clickMarker(m : MapMouseEvent) : void {

			var ll:LatLng = Marker(m.currentTarget).getLatLng();
     		map.setCenter(ll, map.getZoom(), MapType.NORMAL_MAP_TYPE);
     		
     		var i:int = 0;
     		for each(var rm:ResultMarker in markerList) {
     			if(rm.marker.getLatLng().lat() == ll.lat() && rm.marker.getLatLng().lng() == ll.lng()){
     				break;
				}  
 				i++;     				
     		}
     		
     		Marker(m.currentTarget).openInfoWindow(new InfoWindowOptions({title: markerList[i].resultEvent.locationName, content: markerList[i].resultEvent.locationAdress}));
     		
     		ExternalInterface.call("callbackClickMarker", markerList[i].resultEvent.locationId);
		}
		
		public function showEventLocatation(locationId : String) : void {
			var id:int = parseInt(locationId);
			
			clear();
			
			var oneResult:Array = new Array();
			
			for(var i:int=0; i<this.resultList.length; i++){
				if(resultList[i].locationId == id){
					break;
				}
			}
			oneResult.push(this.resultList[i]);
			buildMarkers(oneResult);
		}
		
		public function clear() : void {
			map.clearOverlays(); 
			
			for each(var rm:ResultMarker in markerList) {
				rm.marker.removeEventListener(MapMouseEvent.CLICK, clickMarker);
			}
			markerList = new Array();
			if(locationString=="k"){
				map.setCenter(new LatLng(50.065404096015, 19.94233092907713), 10, MapType.NORMAL_MAP_TYPE);
			}else{
				map.setCenter(new LatLng(52.00331185482757, 18.952424141205015), 6, MapType.NORMAL_MAP_TYPE);
			}
		}		
		
	}
}
